package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.result.Result;
import com.example.entity.vo.Business.InvmabVoListDto;
import com.example.entity.vo.InvmaHeadTree;
import com.example.entity.vo.InvmaHeadTreeVo;
import com.example.entity.vo.InvmaHeadVo;
import com.example.entity.vo.InvmabVo;
import com.example.entity.yifei.Invmb;
import com.example.mapper.CoptdMapper;
import com.example.mapper.InvmbMapper;
import com.example.service.InvmbService;
import com.example.untils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-02-26
 */
@Slf4j
@RestController
@RequestMapping("/api/invmb")
@Api(tags = "品号基本信息接口")
public class InvmbController {

    @Resource
    private InvmbService invmbService;

    @Resource
    private InvmbMapper invmbMapper;

    @Resource
    private CoptdMapper coptdMapper;

    @ApiOperation("品号基本信息vo列表查询")
    @PostMapping("/invmabVoList")
    public Result<PageInfo<InvmabVo>> invmabVoList(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "100")  int pageSize,
            @RequestBody InvmabVoListDto dto){
        PageHelper.startPage(pageNum, pageSize, "MB001");
        if(StringUtils.isNotEmpty(dto.getMB003())) {
            dto.setMB003(dto.getMB003().replaceAll("([\\[\\]*+_%^])", "/$1"));
        }
        if(StringUtils.isNotEmpty(dto.getQuickQuery())) {
            dto.setQuickQuery(dto.getQuickQuery().replaceAll("([\\[\\]*+_%^])", "/$1"));
        }
        List<InvmabVo> invmabVos = invmbMapper.selectInvmabVoList(dto);

        invmabVos.stream().forEach(arr -> {
            Double td010Double = this.coptdMapper.getTd010Double(dto.getCustomId(),arr.getMb001());
            if (arr.getNuclearPrice() == null){
                if (td010Double!=null){
                    arr.setNuclearPrice(td010Double);
                }
            }
            if (td010Double!=null){
                arr.setNuclearPriceLs(td010Double);
            }
        });

        PageInfo<InvmabVo> pageInfo = new PageInfo(invmabVos);
        return Result.success(pageInfo);
    }

    @ApiOperation("品号列表信息头查询  树形结构")
    @GetMapping("/selectInvmaHeadVoTreeList")
    public Result<InvmaHeadTree> selectInvmaHeadVoTreeList(){
        InvmaHeadTree res = new InvmaHeadTree();
        List<InvmaHeadTreeVo> resList = new ArrayList<>();

        List<InvmaHeadVo> invmaHeadVos = invmbMapper.selectInvmaHeadVoTreeList();
        for(InvmaHeadVo vo : invmaHeadVos) {
            InvmaHeadTreeVo aaa = new InvmaHeadTreeVo();
            aaa.setMa002(vo.getMa002());
            aaa.setMa003(vo.getMa003());
            aaa.setHead(invmbMapper.getInvmbByMA002(vo.getMa002()));
            resList.add(aaa);
        }
        res.setTree(resList);
        return Result.success(res);
    }

    @ApiOperation("品号列表信息头查询")
    @GetMapping("/selectInvmaHeadVoList")
    public Result<List<InvmaHeadVo>> selectInvmaHeadVoList(){
        List<InvmaHeadVo> list = invmbMapper.selectInvmaHeadVoList();
        return Result.success(list);
    }

    @ApiOperation("品号列表信息list")
    @GetMapping("/list")
    public Result<IPage<Invmb>> invmbList(@RequestParam(defaultValue = "0") int pageNum,
                                          @RequestParam(defaultValue = "100")  int pageSize){
        Page<Invmb> page = new Page<>(pageNum, pageSize);

        IPage<Invmb> list =
                invmbMapper.selectInvmbList(page);

        return Result.success(list);
    }

}
